<style scoped>
  .buttonMenu {
    position: relative;
    float: left;
  }

</style>
<template>
  <div>
    <div>
      <Button style="float: left; margin-right: 8px" icon="ios-add" type="primary" shape="circle"
        @click="initmodal1()">添加全国专业</Button>
      <Modal v-model="modal1" title="添加全国专业" @on-ok="addNMajor()" width=430 scrollable>
        国家专业代码：
        <Input v-model="NmId" placeholder="输入国家专业代码" style="width: 300px" clearable></Input>
        <br><br>
        <span style="margin-right: 25px">专业名称：</span>
        <Input v-model="NmName" placeholder="输入专业名称" style="width: 300px" clearable></Input>
        <br><br>
        <span style="margin-right: 25px">学历层次：</span>
        <Select v-model="NmLevel" style="width: 300px">
          <Option value="博士">博士</Option>
          <Option value="硕士">硕士</Option>
          <Option value="本科">本科</Option>
          <Option value="专科">专科</Option>
        </Select>
        <br><br>
        <span style="margin-right: 48px">学分：</span>
        <Input v-model="NmCredit" placeholder="输入学分" style="width: 300px" clearable></Input>
      </Modal>
      <Button style="float: left; margin-right: 8px" icon="ios-close" type="error" shape="circle"
        @click="deleteNmajor(delectdata)">删除选中专业</Button>
      <br><br><br>
      <Table border :columns="columnshead" :data="datat" @on-selection-change="delSelect"></Table>
    </div>
    <br>
    <div>
      <div class="buttonMenu">
        <Upload ref="upload" :action="$host + '/professional/management/importNationalMajorExcel'">
          <Button style="margin-right: 8px;" type="success">
            <Icon size="18" type="ios-cloud-upload-outline" />导入</Button>
        </Upload>
      </div>
      <div class="buttonMenu">
        <Button style="margin-right: 8px">
          <Icon size="18" type="ios-download-outline" />
          <a :href="$host + '/professional/management/allNationalwideMajor2ExportExcel'"
            style="color: #515A6E">导出</a></Button>
      </div>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page :total="total" :current="1" show-elevator show-total @on-change="changePage"></Page>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        columnshead: [{
            type: 'selection',
            width: 60,
            align: 'center',
          },
          {
            title: '国家专业代码',
            key: 'nmId',
          },
          {
            title: '专业名称',
            key: 'nmName',
          },
          {
            title: '学历层次',
            key: 'nmLevel',
          },
          {
            title: '学分',
            key: 'nmCredit',
          }

        ],
        datat: [],
        delectdata: [],
        current: 0,
        record: 0,
        total: 1,
        modal1: false,
        NmCredit: '',
        NmId: '',
        NmLevel: '',
        NmName: ''
      }
    },
    created() {
      this.$axios.get(this.$host + '/professional/management/allNationalwideMajor?pageNum=1&&pageSize=10').then(res => {
        this.total = res.data.data.total
        this.datat = res.data.data.list
      })
    },
    methods: {
      changePage(currentPage) {
        this.$axios.get(this.$host + '/professional/management/allNationalwideMajor?pageNum=' + currentPage +
          '&&pageSize=10').then(res => {
          this.datat = res.data.data.list
        })
        this.current = currentPage
      },
      addNMajor() {
        this.$axios.post(this.$host + '/professional/management/newNationalwideMajor', {
          nmId: this.NmId,
          nmName: this.NmName,
          nmLevel: this.NmLevel,
          nmCredit: this.NmCredit

        }).then(res => {
          if (res.data.message == '新增全国专业成功') {
            this.$Message.success('全国专业添加成功')
            if (this.datat.length == 10) {
              this.datat.pop()
            }
            this.datat.unshift({
                nmId: this.NmId,
                nmName: this.NmName,
                nmLevel: this.NmLevel,
                nmCredit: this.NmCredit
              })
              ++this.total
          } else {
            this.$Message.error('专业已存在，添加失败')
          }
        })
      },
      initmodal1() {
        this.modal1 = true;
        this.NmCredit = ''
        this.NmId = ''
        this.NmLevel = ''
        this.NmName = ''
      },
      delSelect(selection) {
        this.delectdata = selection
      },
      deleteNmajor(delectdata) {
        var delectnmId = []
        for (var i in delectdata) {
          delectnmId.push({
            nationwidemajorId: delectdata[i].nmId
          })
          this.record++
        }
        this.$axios.delete(this.$host + '/professional/management/NationalwideMajor', {
          data: {
            jsonArray: delectnmId
          }
        }).then(res => {
          if (res.data.message == '删除全国专业成功') {
            this.$Message.success('成功删除' + this.record + '记录')
            this.record = 0
            this.$axios.get(this.$host + '/professional/management/allNationalwideMajor?pageNum=' + this.current +
              '&&pageSize=10').then(res => {
              this.total = res.data.data.total
              this.datat = res.data.data.list
            })
          } else
            this.$Message.error('删除失败')
        })
      }

    }
  }

</script>
